package cn.broadin.app.hotel.view;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import cn.broadin.app.hotel.R;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;

public class ImageDetailFragment extends Fragment {
	private String mImageUrl;
	private ImageView mImageView;
	private ProgressBar progressBar;

	public static ImageDetailFragment newInstance(String imageUrl) {
		final ImageDetailFragment f = new ImageDetailFragment();

		final Bundle args = new Bundle();
		args.putString("url", imageUrl);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mImageUrl = getArguments() != null ? getArguments().getString("url") : null;

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		final View v = inflater.inflate(R.layout.photo_detail, container, false);
		mImageView = (ImageView) v.findViewById(R.id.image);
		progressBar = (ProgressBar) v.findViewById(R.id.loading);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
		BitmapDisplayConfig displayConfig = new BitmapDisplayConfig();
		bitmapUtils.configDefaultDisplayConfig(displayConfig);
		bitmapUtils.display(mImageView, mImageUrl, new BitmapLoadCallBack<ImageView>(){
			
			@Override
			public void onLoadStarted(ImageView container, String uri, BitmapDisplayConfig config) {
				super.onLoadStarted(container, uri, config);
				progressBar.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadCompleted(ImageView view, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
				progressBar.setVisibility(View.GONE);
				view.setImageBitmap(bitmap);
			}

			@Override
			public void onLoadFailed(ImageView view, String uri, Drawable drawable) {
				Toast.makeText(getActivity(), "图片加载失败！", Toast.LENGTH_SHORT).show();
				progressBar.setVisibility(View.GONE);
			}
			
			
		});
	}
}
